package com.zzqstart.userservice.controller;


import com.zzqstart.commonutils.JwtUtils;
import com.zzqstart.commonutils.MD5;
import com.zzqstart.commonutils.R;
import com.zzqstart.userservice.entity.UcenterMember;
import com.zzqstart.userservice.entity.vo.RegisterVo;
import com.zzqstart.userservice.service.UcenterMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-03-06
 */
@RestController
@RequestMapping("/userservice/member")
@Api(tags = "用户管理中心")
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService ucenterMemberService;

    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public R login(@RequestBody UcenterMember ucenterMember){
        String token = ucenterMemberService.checkUser(ucenterMember);
        return R.ok().data("token",token);
    }

    @ApiOperation(value = "用户注册")
    @PostMapping("/register")
    public R register(@RequestBody RegisterVo registerVo){
        Boolean register = ucenterMemberService.register(registerVo);
        if (register){
            return R.ok();
        }else {
            return R.error().message("注册失败");
        }
    }

    @ApiOperation(value = "通过token获取用户信息")
    @GetMapping("/getMemberInfoByToken")
    public R getMemberInfoByToken(HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember ucenterMember = ucenterMemberService.getMemberById(memberId);
        return R.ok().data("menberInfo",ucenterMember);
    }

    @ApiOperation(value = "通过token获取用户id")
    @GetMapping("/getMemberIdByToken")
    public R getMemberIdByToken(HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        return R.ok().data("memberId",memberId);
    }

    @ApiOperation(value = "通过Id获取用户信息")
    @GetMapping("/getMemberInfoById/{memberId}")
    public R getMemberInfoById(@PathVariable String memberId){
        UcenterMember memberInfo = ucenterMemberService.getById(memberId);
        memberInfo.setPassword("");
        return R.ok().data("memberInfo",memberInfo);
    }

    @ApiOperation(value = "逻辑删除member")
    @DeleteMapping("/deleteMember/{id}")
    public R deletMember(@PathVariable String id){
        UcenterMember member = new UcenterMember();
        member.setId(id);
        member.setIsDisabled(true);
        member.setIsDeleted(true);
        boolean isdelete = ucenterMemberService.updateById(member);
        if (isdelete){
            return R.ok();
        }else {
            return R.error().message("删除失败");
        }
    }

    @ApiOperation(value = "逻辑禁用member")
    @DeleteMapping("/disabledMember/{id}")
    public R disableMember(@PathVariable String id){
        UcenterMember member = new UcenterMember();
        member.setId(id);
        member.setIsDisabled(true);
        boolean isdisable = ucenterMemberService.updateById(member);
        if (isdisable){
            return R.ok();
        }else {
            return R.error().message("禁用失败");
        }
    }

    @ApiOperation(value = "恢复member")
    @PostMapping("/enabledMember/{id}")
    public R enabledMember(@PathVariable String id){
        UcenterMember member = new UcenterMember();
        member.setId(id);
        member.setIsDisabled(false);
        member.setIsDeleted(false);
        boolean enable = ucenterMemberService.updateById(member);
        if (enable){
            return R.ok();
        }else {
            return R.error().message("恢复用户使用失败");
        }
    }

    @ApiOperation(value = "授予用户厨师资格")
    @PostMapping("/changeMemberChef/{id}")
    public R changeMemberChef(@PathVariable String id){
        UcenterMember member = new UcenterMember();
        member.setId(id);
        member.setIsChef(1);
        boolean isUpdate = ucenterMemberService.updateById(member);
        if (isUpdate){
            return R.ok();
        }else {
            return R.error().message("授予失败");
        }
    }

    @ApiOperation(value = "查询全部member")
    @PostMapping("/allMemberPage/{current}/{limit}")
    public R allMemberPage(@PathVariable long current,
                           @PathVariable long limit,
                           @RequestBody UcenterMember member){
        Map<String,Object> map = ucenterMemberService.allMemberPage(current,limit,member);
            return R.ok().data(map);
    }

    @ApiOperation(value = "根据id更新member数据")
    @PostMapping("/updateMemberById")
    public R updateMemberById(@RequestBody UcenterMember member){
        if (!StringUtils.isEmpty(member) && !StringUtils.isEmpty(member.getPassword())){
            String encrypt = MD5.encrypt(member.getPassword());
            member.setPassword(encrypt);
        }
        boolean isUpdate = ucenterMemberService.updateById(member);
        if (isUpdate){
            return R.ok();
        }else {
            return R.error().message("更新失败");
        }
    }

}

